package com.pf.java.ThreadTest;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;

/**
 * 阻塞队列
 *
 * ClassName:Thread11
 * Package:com.pf.java.ThreadTest
 *
 * @author: pf
 * @date: 2021/11/22-14:31
 */
public class Thread11 {

    public static void main(String[] args) {
            BlockingQueue<Long> queue = new ArrayBlockingQueue<>(10);
            new Thread(new Producer(queue), "p1").start();
            new Thread(new Producer(queue), "p2").start();
//            new Thread(new Producer(queue), "p3").start();
//            new Thread(new Producer(queue), "p4").start();
//            new Thread(new Producer(queue), "p5").start();

            new Thread(new Customer(queue), "c1").start();
            new Thread(new Customer(queue), "c2").start();
            new Thread(new Customer(queue), "c3").start();
    }

    private static class Producer implements Runnable {

        private BlockingQueue<Long> queue;


        public Producer(BlockingQueue<Long> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(new Random().nextInt(1000));
                    queue.put(System.currentTimeMillis());
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "生产了一条数据，剩余" + queue.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private static class Customer implements Runnable {

        private BlockingQueue<Long> queue;

        public Customer(BlockingQueue<Long> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(new Random().nextInt(1000));
                    queue.take();
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "消费了一条数据，剩余" + queue.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
